
import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';
import ResourceManager from '../../Runtime/ResourceManager';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, SHAKE_TYPE_ENUM } from '../../Enum';
import EventManager from '../../Runtime/EventManager';
import { PlayerStateMachine } from './PlayerStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import DataManager from '../../Runtime/DataManager';
import { IEntity } from '../../Levels';
import { BurstManager } from '../Burst/BurstManager';
import { EnemyManager } from '../../Base/EnemyManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerManager
 * DateTime = Tue Jun 27 2023 13:04:45 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = PlayerManager.ts
 * FileBasenameNoExtension = PlayerManager
 * URL = db://assets/Script/Player/PlayerManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */


const ABIMATION_SPEED = 1 / 8  //帧数  8帧
@ccclass('PlayerManager')
export class PlayerManager extends EntityManager {

    // x:number=0
    // y:number=0
    targetX: number
    targetY: number
    private readonly speed = 1 / 10
    isMoving = false

    // fsm:PlayerStateMachine


    // //人物当前的状态
    // private _state:ENTITY_STATE_ENUM
    // //人物当前的方向
    // private  _driection:DIRECTION_ENUM


    // //数据ui分离
    // get direction(){
    //     return this._driection
    // }


    // set direction(newDirection){
    //     this._driection=newDirection
    //     this.fsm.setParams(PARAMS_NAME_ENUM.DIRECTION,DIRECTION_ORDER_ENUM[this._driection])
    // }

    // get state(){
    //     return this.state
    // }


    // set state(newState){
    //     this._state=newState
    //     this.fsm.setParams(this._state,true)
    // }


    update() {

        this.updateXY()

        super.update()


        // this.node.setPosition(this.x*TILE_WIDTH-TILE_WIDTH*1.5,this.y*TILE_HEIGHT+TILE_HEIGHT*1.5)
    }

    async init(params: IEntity) {
        // //拆的代码
        // const sprite = this.addComponent(Sprite)
        // sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小

        // const ui=this.getComponent(UITransform)
        // ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)



        //控制turnleft组件
        this.fsm = this.addComponent(PlayerStateMachine)
        await this.fsm.init() //初始化状态机


        super.init(params)

        this.targetX = this.x
        this.targetY = this.y

        // //设置初始方向
        // this.direction=DIRECTION_ENUM.TOP
        // //设置当前人物状态
        // this.state=ENTITY_STATE_ENUM.IDLE


        //await this.render()
        EventManager.Instance.on(EVENT_ENUM.PLAYER_CTRL, this.inputHandle, this)

        EventManager.Instance.on(EVENT_ENUM.ATTACK_PLAYER, this.onDead, this)







    }

    onDestroy() {
        super.onDestroy()
        EventManager.Instance.off(EVENT_ENUM.PLAYER_CTRL, this.inputHandle)
        EventManager.Instance.off(EVENT_ENUM.ATTACK_PLAYER, this.onDead)
    }

    onDead(type: ENTITY_STATE_ENUM) {

        console.log(this)
        if (this.fsm === null) {

            console.log("nuuuuuu")

        }
        console.log("onDead")
        console.log(type + "onDead")

        //EVENT_ENUM.ATTACK_PLAYER
        this.state = type

    }

    onAttackShake(type: SHAKE_TYPE_ENUM) {
        console.log("onAttackShake")
        EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, type)
    }

    //让xy趋近目标
    updateXY() {
        if (this.targetX < this.x) {
            this.x -= this.speed
        } else if (this.targetX > this.x) {
            this.x += this.speed
        }



        if (this.targetY < this.y) {
            this.y -= this.speed
        } else if (this.targetY > this.y) {
            this.y += this.speed
        }


        if (Math.abs(this.targetX - this.x) <= 0.1 && Math.abs(this.targetY - this.y) <= 0.1 && this.isMoving) {
            this.isMoving = false
            this.x = this.targetX
            this.y = this.targetY
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        }
    }


    //处理用户输入
    inputHandle(inputDirection: CONTROLLER_ENUM) {
        if (this.isMoving) {
            return
        }
        if (this.state === ENTITY_STATE_ENUM.DEATH || this.state === ENTITY_STATE_ENUM.AIRDEATH || this.state === ENTITY_STATE_ENUM.ATTACK) {
            return   //死亡不能移动
        }

        const id = this.willAttack(inputDirection)
        if (id) {

            EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)
            this.state = ENTITY_STATE_ENUM.ATTACK

            EventManager.Instance.emit(EVENT_ENUM.ATTACK_ENMEY, id)
            EventManager.Instance.emit(EVENT_ENUM.DOOR_OPEN)

            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)

            return
        }


        if (this.willBlock(inputDirection)) {  //返回true


            //撞墙震动效果

            if (inputDirection === CONTROLLER_ENUM.TOP) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.TOP)
            } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.BOTTOM)
            } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.LEFT)
            } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.RIGHT)
            } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT && this.direction === DIRECTION_ENUM.TOP) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.LEFT)
            } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT && this.direction === DIRECTION_ENUM.LEFT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.BOTTOM)
            } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT && this.direction === DIRECTION_ENUM.BOTTOM) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.RIGHT)
            } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT && this.direction === DIRECTION_ENUM.RIGHT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.TOP)
            } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT && this.direction === DIRECTION_ENUM.TOP) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.RIGHT)
            } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT && this.direction === DIRECTION_ENUM.LEFT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.TOP)
            } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT && this.direction === DIRECTION_ENUM.BOTTOM) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.LEFT)
            } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT && this.direction === DIRECTION_ENUM.RIGHT) {
                EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE, SHAKE_TYPE_ENUM.BOTTOM)
            }







            console.log("撞墙")
            return       //撞了就不执行了
        }
        this.move(inputDirection)
    }


    willAttack(type: CONTROLLER_ENUM) {
        const enemies = DataManager.Instance.enemies.filter(enemy => enemy.state !== ENTITY_STATE_ENUM.DEATH)
        for (let i = 0; i < enemies.length; i++) {
            const { x: enemyx, y: enemyy, id: enemyid } = enemies[i];
            if (type === CONTROLLER_ENUM.TOP && this.direction === DIRECTION_ENUM.TOP && enemyx === this.x && enemyy === this.targetY - 2) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyid
            } else if (type === CONTROLLER_ENUM.LEFT && this.direction === DIRECTION_ENUM.LEFT && enemyx === this.x - 2 && enemyy === this.targetY) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyid
            } else if (type === CONTROLLER_ENUM.BOTTOM && this.direction === DIRECTION_ENUM.BOTTOM && enemyx === this.x && enemyy === this.targetY + 2) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyid
            } else if (type === CONTROLLER_ENUM.RIGHT && this.direction === DIRECTION_ENUM.RIGHT && enemyx === this.x + 2 && enemyy === this.targetY) {
                this.state = ENTITY_STATE_ENUM.ATTACK
                return enemyid
            }
        }
        return ''
    }

    willBlock(inputDirection: CONTROLLER_ENUM) {
        const { mapRowCount: row, mapColumnCount: column } = DataManager.Instance
        //拿自己的数据
        const { targetX: x, targetY: y, direction } = this
        //拿瓦片信息
        const { tileInfo } = DataManager.Instance

        const { x: doorX, y: doorY, state: doorState } = DataManager.Instance.door
        const enemies: EnemyManager[] = DataManager.Instance.enemies.filter((enemy: EnemyManager) => enemy.state !== ENTITY_STATE_ENUM.DEATH)

        const bursts: BurstManager[] = DataManager.Instance.bursts.filter((enemy: BurstManager) => enemy.state !== ENTITY_STATE_ENUM.DEATH)





        if (inputDirection === CONTROLLER_ENUM.TOP) {
            //下一个y坐标  注意游戏的xy坐标不同
            const playerNextY = y - 1


            //当人物朝上的时候，并且输入上的时候 ，人物会不会撞
            if (direction === DIRECTION_ENUM.TOP) {



                const weqponNexty = y - 2

                //判断人有没有走出地图
                if (playerNextY < 0) {
                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }



                //拿到下两个瓦片
                //人物的下一个瓦片
                const playerTile = tileInfo[x][playerNextY]
                //枪的下一个瓦片
                const weqponTile = tileInfo[x][weqponNexty]

                //判断门的碰撞
                if (((x === doorX && playerNextY === doorY) || (x === doorX && weqponNexty === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                //判断敌人碰撞
                for (let i = 0; i < enemies.length; i++) {
                    const { x: enemyx, y: enemyy, state: enemtstate } = enemies[i]
                    if ((x === enemyx && playerNextY === enemyy) || (x === enemyx && weqponNexty === enemyy)) {

                        this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                        return true
                    }
                }


                //判断地裂
                for (let i = 0; i < bursts.length; i++) {
                    const { x: burstx, y: bursty } = bursts[i]
                    if ((x === burstx && playerNextY === bursty) && (!weqponTile || weqponTile.turnable)) {
                        return false  //不能走
                    }
                }

                //判断能不能走
                if (playerTile && playerTile.moveable && (!weqponTile || weqponTile.turnable)) {
                    //能走
                } else {
                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }


            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                const weaponNextY = y
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]


                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === x && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === x && enemyY === playerNextY) {
                        this.state = ENTITY_STATE_ENUM.BLOCKBACK
                        return true
                    }
                }


                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //判断能不能走
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }
            } else if (direction === DIRECTION_ENUM.LEFT) {
                //判断是否超出地图
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]


                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }


                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }
                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

            } else if (direction === DIRECTION_ENUM.RIGHT) {
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }


                const weaponNextX = x + 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                        return true
                    }
                }

                // 判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }


                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

            }
        } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
            const playerNextY = y + 1

            if (direction === DIRECTION_ENUM.TOP) {
                if (playerNextY > column - 1) {


                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                const weaponNextY = y
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]


                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === x && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === x && enemyY === playerNextY) {
                        this.state = ENTITY_STATE_ENUM.BLOCKBACK
                        return true
                    }
                }

                // 判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                //判断是否超出地图
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                const weaponNextY = y
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[x]?.[weaponNextY]



                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === x && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                // 判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === weaponNextY) || (enemyX === x && enemyY === playerNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }
            }
            else if (direction === DIRECTION_ENUM.LEFT) {

                //判断是否超出地图
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]




                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

            }
            else if (direction === DIRECTION_ENUM.RIGHT) {
                //判断是否超出地图
                if (playerNextY < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[x]?.[playerNextY]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]




                //判断门
                if (
                    ((doorX === x && doorY === playerNextY) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === x && enemyY === playerNextY) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === x && burst.y === playerNextY) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }


                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {


                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

            }
        } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
            const playerNextX = x - 1
            if (direction === DIRECTION_ENUM.TOP) {
                //判断是否超出地图
                if (playerNextX < 0) {


                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }
                const weaponNextX = x - 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }


                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

            } else if (direction === DIRECTION_ENUM.BOTTOM) {

                //判断是否超出地图
                if (playerNextX < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                const weaponNextX = x - 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]



                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }
            }
            else if (direction === DIRECTION_ENUM.LEFT) {
                //判断是否超出地图
                if (playerNextX < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                const weaponNextX = x - 2
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]


                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === y)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }
            }
            else if (direction === DIRECTION_ENUM.RIGHT) {
                //判断是否超出地图
                if (playerNextX < 0) {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                const weaponNextX = x
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === playerNextX && enemyY === y) {
                        this.state = ENTITY_STATE_ENUM.BLOCKBACK
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }
            }
        } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
            const playerNextX = x + 1

            if (direction === DIRECTION_ENUM.TOP) {
                if (playerNextX > row - 1) {

                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y - 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKRIGHT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }


                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {
                    return true
                }

            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                if (playerNextX > row - 1) {


                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                const weaponNextX = x + 1
                const weaponNextY = y + 1
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[weaponNextY]


                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === weaponNextY)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === weaponNextY)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }

                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKLEFT
                    return true
                }

            }
            else if (direction === DIRECTION_ENUM.LEFT) {
                if (playerNextX > row - 1) {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                const weaponNextX = x
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]


                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if (enemyX === playerNextX && enemyY === y) {
                        this.state = ENTITY_STATE_ENUM.BLOCKBACK
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }



                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKBACK
                    return true
                }
            }
            else if (direction === DIRECTION_ENUM.RIGHT) {

                if (playerNextX > row - 1) {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                const weaponNextX = x + 2
                const nextPlayerTile = tileInfo[playerNextX]?.[y]
                const nextWeaponTile = tileInfo[weaponNextX]?.[y]

                //判断门
                if (
                    ((doorX === playerNextX && doorY === y) || (doorX === weaponNextX && doorY === y)) &&
                    doorState !== ENTITY_STATE_ENUM.DEATH
                ) {
                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

                //判断敌人
                for (let i = 0; i < enemies.length; i++) {
                    const enemy = enemies[i]
                    const { x: enemyX, y: enemyY } = enemy

                    if ((enemyX === playerNextX && enemyY === y) || (enemyX === weaponNextX && enemyY === y)) {
                        this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                        return true
                    }
                }

                //判断地裂陷阱
                if (
                    bursts.some(burst => burst.x === playerNextX && burst.y === y) &&
                    (!nextWeaponTile || nextWeaponTile.turnable)
                ) {
                    return false
                }


                //最后判断地图元素
                if (nextPlayerTile && nextPlayerTile.moveable && (!nextWeaponTile || nextWeaponTile.turnable)) {
                    // empty
                } else {

                    this.state = ENTITY_STATE_ENUM.BLOCKFRONT
                    return true
                }

            }

        } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {
            let nextY, nextX
            if (direction === DIRECTION_ENUM.TOP) {
                //朝上左转的话，左上角三个tile都必须turnable为true，并且没有敌人
                nextY = y - 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                nextY = y + 1
                nextX = x + 1
            } else if (direction === DIRECTION_ENUM.LEFT) {
                nextY = y + 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                nextY = y - 1
                nextX = x + 1
            }

            //判断门的碰撞
            if (((x === doorX && nextY === doorY) || (nextX === doorX && y === doorY) || (nextX === doorX && nextY === doorY)) && doorState !== ENTITY_STATE_ENUM.DEATH) {

                this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT
                return true
            }




            //判断敌人
            for (let i = 0; i < enemies.length; i++) {
                const enemy = enemies[i]
                const { x: enemyX, y: enemyY } = enemy

                if (enemyX === nextX && enemyY === y) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT

                    return true
                } else if (enemyX === nextX && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT

                    return true
                } else if (enemyX === x && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT

                    return true
                }
            }



            // //判断敌人碰撞
            // for (let i = 0; i < enemies.length; i++) {
            //     const { x: enemyx, y: enemyy, state: enemtstate } = enemies[i]
            //     if (((x === enemyx && nextY === enemyy) || (nextX === enemyx && y === nextY) || (nextX === enemyx && nextY === nextY))) {

            //         this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT
            //         return true
            //     }
            // }


            //最后判断地图元素
            if (
                (!tileInfo[x]?.[nextY] || tileInfo[x]?.[nextY].turnable) &&
                (!tileInfo[nextX]?.[y] || tileInfo[nextX]?.[y].turnable) &&
                (!tileInfo[nextX]?.[nextY] || tileInfo[nextX]?.[nextY].turnable)
            ) {
                // empty
            } else {
                this.state = ENTITY_STATE_ENUM.BLOCKTURNLEFT
                return true
            }
        } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT) {
            let nextX, nextY
            if (direction === DIRECTION_ENUM.TOP) {
                //朝上右转的话，右上角三个tile都必须turnable为true
                nextY = y - 1
                nextX = x + 1
            } else if (direction === DIRECTION_ENUM.BOTTOM) {
                nextY = y + 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.LEFT) {
                nextY = y - 1
                nextX = x - 1
            } else if (direction === DIRECTION_ENUM.RIGHT) {
                nextY = y + 1
                nextX = x + 1
            }

            //判断门
            if (
                ((doorX === x && doorY === nextY) ||
                    (doorX === nextX && doorY === y) ||
                    (doorX === nextX && doorY === nextY)) &&
                doorState !== ENTITY_STATE_ENUM.DEATH
            ) {
                this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT
                return true
            }

            //判断敌人
            for (let i = 0; i < enemies.length; i++) {
                const enemy = enemies[i]
                const { x: enemyX, y: enemyY } = enemy

                if (enemyX === nextX && enemyY === y) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT

                    return true
                } else if (enemyX === nextX && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT

                    return true
                } else if (enemyX === x && enemyY === nextY) {
                    this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT

                    return true
                }
            }

            //最后判断地图元素
            if (
                (!tileInfo[x]?.[nextY] || tileInfo[x]?.[nextY].turnable) &&
                (!tileInfo[nextX]?.[y] || tileInfo[nextX]?.[y].turnable) &&
                (!tileInfo[nextX]?.[nextY] || tileInfo[nextX]?.[nextY].turnable)
            ) {
                // empty
            } else {

                this.state = ENTITY_STATE_ENUM.BLOCKTURNRIGHT
                return true
            }
        }
        return false

    }

    //实现人物的移动,参数为方向的类型
    move(inputDirection: CONTROLLER_ENUM) {
        EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)  //记录 

        // console.log(DataManager.Instance.tileInfo)
        if (inputDirection === CONTROLLER_ENUM.TOP) {
            this.targetY -= 1
            console.log("上")
            this.isMoving = true

        } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
            this.targetY += 1
            this.isMoving = true
        } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
            this.targetX -= 1
            this.isMoving = true
        } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
            this.targetX += 1
            this.isMoving = true
        } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {

            if (this.direction === DIRECTION_ENUM.TOP) {
                this.direction = DIRECTION_ENUM.LEFT
            } else if (this.direction === DIRECTION_ENUM.LEFT) {
                this.direction = DIRECTION_ENUM.BOTTOM
            } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                this.direction = DIRECTION_ENUM.RIGHT
            } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                this.direction = DIRECTION_ENUM.TOP

            }
            this.state = ENTITY_STATE_ENUM.TURNLEFT
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)


        } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT) {
            if (this.direction === DIRECTION_ENUM.TOP) {
                this.direction = DIRECTION_ENUM.RIGHT
            } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
                this.direction = DIRECTION_ENUM.LEFT
            } else if (this.direction === DIRECTION_ENUM.LEFT) {
                this.direction = DIRECTION_ENUM.TOP
            } else if (this.direction === DIRECTION_ENUM.RIGHT) {
                this.direction = DIRECTION_ENUM.BOTTOM
            }
            this.state = ENTITY_STATE_ENUM.TURNRIGHT
            EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        }


    }



    // //实现攻击震动，参数为震动的方向
    // onAttackShake(type:SHAKE_TYPE_ENUM){
    //     console.log("onAttackShake")
    //     EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,type)

    // }


    //渲染
    // async render(){
    //     const sprite = this.addComponent(Sprite)
    //     sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小

    //     const ui=this.getComponent(UITransform)
    //     ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)


    //     //加载资源
    //     const spriteFrames=await ResourceManager.Instance.loadDir("texture/player/idle/top")

    //     const animationsComponent=this.addComponent(Animation)

    //     //甚至clip
    //     const animationClip = new AnimationClip()


    //     //轨道
    //     const track =new animation.ObjectTrack()//创建对象轨道
    //     track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame'); // 指定轨道路径，即指定目标对象为 "Foo" 子节点的 "position" 属性


    //     //处理资源数组
    //     const frams :   Array<[number,SpriteFrame]>=spriteFrames.map((item,index)=>[ABIMATION_SPEED*index,item]) //得到关键帧列表
    //     track.channel.curve.assignSorted(
    //         frams
    // //         [ // 为 x 通道的曲线添加关键帧
    // //     [0.4, ({ value: 0.4 })],
    // //     [0.6, ({ value: 0.6 })],
    // //     [0.8, ({ value: 0.8 })],
    // // ]
    // )

    //     //最后将轨道添加到动画剪辑
    //     animationClip.addTrack(track)

    //     animationClip.wrapMode=AnimationClip.WrapMode.Loop

    //     //
    //     animationClip.duration=frams.length*ABIMATION_SPEED //整个动画剪辑的周期
    //     animationsComponent.defaultClip=animationClip
    //     animationsComponent.play()
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
